<?php

class BookingForm
{
	private $BookingFormID;
	private $SNAPEventID;
	private $GeneralHTML;
	private $VergersHTML;
	private $MusicHTML;
	private $FinanceHTML;
	private $CateringHTML;
	private $PublicityHTML;
	private $TimetableHTML;
	private $SafeguardingHTML;
	private $LiturgyHTML;
	
	public function __construct($SNAPEventID)
	{
		$this->SNAPEventID = $SNAPEventID;
		$database = $GLOBALS['DATABASE']->Recopy();
		
		$sql = "SELECT EventID, EventBookingFormID, General, Vergers, Music, Finance, Catering, Publicity, Timetable, Safeguarding, Liturgy ";
		$sql .= "FROM EventBookingForm ";
		$sql .= "WHERE EventID = ".$this->SNAPEventID;
		
		$database->executeSQL($sql);
		$row=$database->getRow();
		
		$this->BookingFormID       = $row['EventBookingFormID'];
		
		$this->GeneralHTML         = $row['General'];
		$this->VergersHTML         = $row['Vergers'];
		$this->MusicHTML           = $row['Music'];
		$this->FinanceHTML         = $row['Finance'];
		$this->CateringHTML        = $row['Catering'];
		$this->PublicityHTML       = $row['Publicity'];
		$this->TimetableHTML       = $row['Timetable'];
		$this->SafeguardingHTML    = $row['Safeguarding'];
		$this->LiturgyHTML   	   = $row['Liturgy'];
	}
	
	public function init()
	{
		$this->setGeneralHTML("<h2>Title</h2><p>Text</p>");
		$this->setVergersHTML("<h2>Title</h2><p>Text</p>");
		$this->setMusicHTML("<h2>Title</h2><p>Text</p>");
		$this->setFinanceHTML("<h2>Title</h2><p>Text</p>");
		$this->setCateringHTML("<h2>Title</h2><p>Text</p>");
		$this->setPublicityHTML("<h2>Title</h2><p>Text</p>");
		$this->setTimetableHTML("<h2>Title</h2><p>Text</p>");
		$this->setSafeguardingHTML("<h2>Title</h2><p>Text</p>");
		$this->setLiturgyHTML("<h2>Title</h2><p>Text</p>");	
		$this->save();	
	}
	
	public function getBookingFormID() 		 {return $this->BookingFormID;}
	public function getSNAPEventID()         {return $this->SNAPEventID;}
	public function getGeneralHTML()         {return $this->GeneralHTML;}
	public function getVergersHTML()         {return $this->VergersHTML;}
	public function getMusicHTML()           {return $this->MusicHTML;}
	public function getFinanceHTML()         {return $this->FinanceHTML;}
	public function getCateringHTML()        {return $this->CateringHTML;}
	public function getPublicityHTML()       {return $this->PublicityHTML;}
	public function getTimetableHTML()       {return $this->TimetableHTML;}
	public function getSafeguardingHTML()    {return $this->SafeguardingHTML;}
	public function getLiturgyHTML()         {return $this->LiturgyHTML;}
	
	
	public function setGeneralHTML($html)		{$this->GeneralHTML=$html;}
	public function setVergersHTML($html)		{$this->VergersHTML=$html;}
	public function setMusicHTML($html)			{$this->MusicHTML=$html;}
	public function setFinanceHTML($html)		{$this->FinanceHTML=$html;}
	public function setCateringHTML($html)		{$this->CateringHTML=$html;}
	public function setPublicityHTML($html)		{$this->PublicityHTML=$html;}
	public function setTimetableHTML($html)		{$this->TimetableHTML=$html;}
	public function setSafeguardingHTML($html)	{$this->SafeguardingHTML=$html;}
	public function setLiturgyHTML($html)		{$this->LiturgyHTML=$html;}
	
	public function save()
	{
		$database = $GLOBALS['DATABASE']->Recopy();
		
		// if entry doesn't exist, create it
		if($this->BookingFormID=='')
		{
			$sql = "INSERT INTO EventBookingForm (EventID,General,Vergers,Music,Finance,Catering,Publicity,Timetable,Safeguarding,Liturgy) ";
			$sql .= "VALUES(";
			$sql .= "'".$this->SNAPEventID."',";
			$sql .= "'".addslashes($this->GeneralHTML)."',";
			$sql .= "'".addslashes($this->VergersHTML)."',";
			$sql .= "'".addslashes($this->MusicHTML)."',";
			$sql .= "'".addslashes($this->FinanceHTML)."',";
			$sql .= "'".addslashes($this->CateringHTML)."',";
			$sql .= "'".addslashes($this->PublicityHTML)."',";
			$sql .= "'".addslashes($this->TimetableHTML)."',";
			$sql .= "'".addslashes($this->SafeguardingHTML)."',";
			$sql .= "'".addslashes($this->LiturgyHTML)."')";
		}
		else
		{
			$sql = "UPDATE EventBookingForm SET ";
			$sql .= "General='".addslashes($this->GeneralHTML)."',";		
			$sql .= "Vergers='".addslashes($this->VergersHTML)."',";		
			$sql .= "Music='".addslashes($this->MusicHTML)."',";		
			$sql .= "Finance='".addslashes($this->FinanceHTML)."',";		
			$sql .= "Catering='".addslashes($this->CateringHTML)."',";		
			$sql .= "Publicity='".addslashes($this->PublicityHTML)."',";		
			$sql .= "Timetable='".addslashes($this->TimetableHTML)."',";		
			$sql .= "Safeguarding='".addslashes($this->SafeguardingHTML)."',";
			$sql .= "Liturgy='".addslashes($this->LiturgyHTML)."' ";
			$sql .= "WHERE EventID = ".addslashes($this->SNAPEventID);	
		}
//		echo $sql;
		$database->ExecuteSQL($sql);	
	}
	
	public function createWordDocument($html)
	{
		
		
		 
		// New Word Document:
		$phpword_object = new PHPWord();
		$section = $phpword_object->createSection();
		
		// HTML Dom object:
		$html_dom = new simple_html_dom();
		$html_dom->load('<html><body>' . $html . '</body></html>');
		// Note, we needed to nest the html in a couple of dummy elements.
		
		// Create the dom array of elements which we are going to work on:
		$html_dom_array = $html_dom->find('html',0)->children();
		
		// We need this for setting base_root and base_path in the initial_state array
		// (below). We are using a function here (derived from Drupal) to create these
		// paths automatically - you may want to do something different in your
		// implementation. This function is in the included file 
		// documentation/support_functions.inc.
		$paths = htmltodocx_paths();
		
		
		
		// Provide some initial settings:
		$initial_state = array(
		  // Required parameters:
		  'phpword_object' => &$phpword_object, // Must be passed by reference.
		  'base_root' => 'http://test1.stnicholascathedralmusic.co.uk', // Required for link elements - change it to your domain.
		  'base_path' => '/snap/library/htmltodocx/', // Path from base_root to whatever url your links are relative to.
		//  'base_root' => $paths['base_root'],
		//  'base_path' => $paths['base_path'],
		  // Optional parameters - showing the defaults if you don't set anything:
		  'current_style' => array('size' => '11'), // The PHPWord style on the top element - may be inherited by descendent elements.
		  'parents' => array(0 => 'body'), // Our parent is body.
		  'list_depth' => 0, // This is the current depth of any current list.
		  'context' => 'section', // Possible values - section, footer or header.
		  'pseudo_list' => TRUE, // NOTE: Word lists not yet supported (TRUE is the only option at present).
		  'pseudo_list_indicator_font_name' => 'Wingdings', // Bullet indicator font.
		  'pseudo_list_indicator_font_size' => '7', // Bullet indicator size.
		  'pseudo_list_indicator_character' => 'l ', // Gives a circle bullet point with wingdings.
		  'table_allowed' => TRUE, // Note, if you are adding this html into a PHPWord table you should set this to FALSE: tables cannot be nested in PHPWord.
		  'treat_div_as_paragraph' => TRUE, // If set to TRUE, each new div will trigger a new line in the Word document.
		      
		  // Optional - no default:    
		  'style_sheet' => htmltodocx_styles_example(), // This is an array (the "style sheet") - returned by htmltodocx_styles_example() here (in styles.inc) - see this function for an example of how to construct this array.
		  );    
		
		
		// Convert the HTML and put it into the PHPWord object
		htmltodocx_insert_html($section, $html_dom_array[0]->nodes, $initial_state);
		
		// Clear the HTML dom object:
		$html_dom->clear(); 
		unset($html_dom);
		
		// Save File
		$h2d_file_uri = tempnam('/tmp', 'htd');
		$objWriter = PHPWord_IOFactory::createWriter($phpword_object, 'Word2007');
		$objWriter->save($h2d_file_uri);
		
		// Download the file:
		header('Content-Description: File Transfer');
		header('Content-Type: application/octet-stream');
		header('Content-Disposition: attachment; filename=example.docx');
		header('Content-Transfer-Encoding: binary');
		header('Expires: 0');
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
		header('Pragma: public');
		header('Content-Length: ' . filesize($h2d_file_uri));
		ob_clean();
		flush();
		$status = readfile($h2d_file_uri);
		unlink($h2d_file_uri);
		exit;		
	}
		
}

